/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.commons.collections;

import java.util.ArrayList;


/**
 * NOT Threadsafe.
 */
public class StagingList<E> {

    private static final int STAGING_SIZE_REMOVE = 30;
    private static final int STAGING_SIZE_ADD = 30;
    private static final int SIZE = 30;

    private final ArrayList<E> toAdd;
    private final ArrayList<E> toRemove;
    private final ArrayList<E> elements;

    public StagingList(int size) {
        this(size, STAGING_SIZE_ADD, STAGING_SIZE_REMOVE);
    }

    public StagingList(int size, int addSize, int removeSize) {
        toAdd = new ArrayList<E>(addSize);
        elements = new ArrayList<E>(size);
        toRemove = new ArrayList<E>(removeSize);
    }

    public StagingList() {
        this(SIZE, STAGING_SIZE_ADD, STAGING_SIZE_REMOVE);
    }

    public void applyChanges() {

        for (int i = 0; i < toRemove.size(); i++) {
            elements.remove(toRemove.get(i));
        }
        toRemove.clear();
        for (int i = 0; i < toAdd.size(); i++) {
            elements.add(toAdd.get(i));

        }
        toAdd.clear();

    }

    public final void add(E object) {
        toAdd.add(object);
    }

    public void remove(E object) {
        toRemove.add(object);
    }

    public void clear() {
        elements.clear();
        toAdd.clear();
        toRemove.clear();
    }

    public boolean contains(E object) {
        return elements.contains(object);
    }

    public E get(int index) {
        return elements.get(index);
    }

    public ArrayList<E> getElements() {
        return elements;
    }
}